package tables

import (
	"fmt"

	"github.com/GoAdminGroup/go-admin/context"
	"github.com/GoAdminGroup/go-admin/modules/db"
	"github.com/GoAdminGroup/go-admin/plugins/admin/modules/table"
	"github.com/GoAdminGroup/go-admin/template/icon"
	"github.com/GoAdminGroup/go-admin/template/types/action"
	"github.com/GoAdminGroup/go-admin/template/types/form"
)

func GetFileTable(ctx *context.Context) table.Table {

	file := table.NewDefaultTable(ctx, table.DefaultConfigWithDriver("mysql"))

	info := file.GetInfo()
	info.HideDeleteButton() //隐藏删除按钮
	info.HideDetailButton() //隐藏详情按钮
	info.HideNewButton()    //隐藏新增按钮
	info.HideEditButton()   //隐藏编辑按钮

	info.AddField("Id", "id", db.Int)
	info.AddField("Name", "name", db.Varchar)
	info.AddField("Size", "size", db.Int)
	info.AddField("Path", "path", db.Varchar)
	info.AddField("Created_at", "created_at", db.Int)
	info.AddField("Updated_at", "updated_at", db.Int)
	info.AddField("Deleted_at", "deleted_at", db.Int)

	info.AddButton(ctx, "文件上传", icon.Upload, action.PopUpWithIframe("upload", "文件上传", action.IframeData{Src: "/upload"}, "900px", "560px"))
	info.AddActionButton(ctx, "下载", action.PopUp("/admin/download", "下载",
		func(ctx *context.Context) (success bool, msg string, data interface{}) {
			// 下载功能未实现
			x := ctx.FormValue("filename")
			fmt.Println("filename", x)
			return true, "", "<h2>下载成功</h2>"
		}))

	info.SetTable("files").SetTitle("文件中心").SetDescription("文件中心")

	formList := file.GetForm()
	formList.AddField("Id", "id", db.Int, form.Default).
		FieldDisableWhenCreate()
	formList.AddField("Name", "name", db.Varchar, form.Text)
	formList.AddField("Size", "size", db.Int, form.Number)
	formList.AddField("Path", "path", db.Varchar, form.Text)
	formList.AddField("Created_at", "created_at", db.Int, form.Datetime)
	formList.AddField("Updated_at", "updated_at", db.Int, form.Datetime)
	formList.AddField("Deleted_at", "deleted_at", db.Int, form.Datetime).FieldHide()

	formList.SetTable("files").SetTitle("文件中心").SetDescription("文件中心")

	return file
}
